package com.androider.timememory.db;

import com.androider.timememory.utils.Dict;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Contacts.Phones;

public class RecordDAO {
    private Context mContext;
    private static RecordDAO recordDAO = null;
    private DataBaseHelper database = null;
    private SQLiteDatabase db = null;
    
    private RecordDAO(Context context) {
        mContext = context;
    }
    
    public static RecordDAO getInstance(Context context) {
        if(null == recordDAO) {
            recordDAO = new RecordDAO(context);
        }
        return recordDAO;
    }
    
    public void open() {
        database = new DataBaseHelper(mContext);
        db = database.getWritableDatabase();
    }
    
    public void close() {
        db.close();
    }
    
    public boolean insert(String table, ContentValues value) {
        try {
            if(-1 != db.insert(table, null, value)) {
            	return true;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public Cursor query() {
    	String[] columns = {Dict.C_id, Dict.C_category, Dict.C_type, Dict.C_content, Dict.C_date, Dict.C_note};
        Cursor c = db.query(Dict.T_record, columns, null, null, null, null, null);
        return c;
    }
    
    public Cursor queryById(int id) {
    	Cursor c = db.query(Dict.T_record, null, Dict.C_id + "=?", new String[]{String.valueOf(id)}, null, null, null);
    	return c;
    }
    
    public Cursor queryByType(String type) {
    	String[] columns = {Dict.C_id, Dict.C_category, Dict.C_type, Dict.C_content, Dict.C_date, Dict.C_note};
    	Cursor c = db.query(Dict.T_record, columns, Dict.C_type + " =", new String[]{type}, null, null, null);
    	return c;
    }
    
    public Cursor queryImageRecord() {
    	String[] columns = {Dict.C_id, Dict.C_category, Dict.C_type, Dict.C_content, Dict.C_date, Dict.C_note};
    	Cursor c = db.query(Dict.T_record, columns, Dict.C_type + " =? or " + Dict.C_type + " =?", new String[]{"1", "2"}, null, null, null);
    	return c;
    }
    
    public Cursor queryByCategory(String category) {
    	String[] columns = {Dict.C_id, Dict.C_category, Dict.C_type, Dict.C_content, Dict.C_date, Dict.C_note};
        Cursor c = db.query(Dict.T_record, columns, Dict.C_category + " like ?", new String[]{"%" + category + "%"}, null, null, null);
        return c;
    }
    
    public Cursor searchByNote(String queryString) {
    	String[] columns = {Dict.C_id, Dict.C_category, Dict.C_type, Dict.C_content, Dict.C_date, Dict.C_note};
        Cursor c = db.query(Dict.T_record, columns, Dict.C_note + " like ?", new String[]{"%" + queryString + "%"}, null, null, null);
        return c;
    }
    
    public Cursor queryWhiteList() {
    	String[] columns = {Dict.C_name, Dict.C_number};
        Cursor c = db.query(Dict.T_whitelist, columns, null, null, null, null, null);
        return c;
    }
    
    public void deleteAll(String table) {
    	db.delete(table, null, null);
    }
    
    public Cursor queryContacts() {
    	Cursor c = mContext.getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME + " ASC");
    	return c;
    }
    
}
